import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import qs from 'qs'


import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';


import 'font-awesome/css/font-awesome.min.css'
import {
    getCurDate,
    setSessionStorage,
    getSessionStorage,
    removeSessionStorage,
    setLocalStorage,
    getLocalStorage,
    removeLocalStorage
}   from './common.js'

//外部包挂载到vue原型上
Vue.prototype.$axios = axios
Vue.prototype.$qs = qs
//挂载公有函数
Vue.prototype.$getCurDate = getCurDate
Vue.prototype.$setSessionStorage = setSessionStorage
Vue.prototype.$getSessionStorage = getSessionStorage
Vue.prototype.$removeSessionStorage = removeSessionStorage
Vue.prototype.$setLocalStorage = setLocalStorage
Vue.prototype.$getLocalStorage = getLocalStorage
Vue.prototype.$removeLocalStorage = removeLocalStorage

// axios.defaults.baseURL = "http://localhost:8088/elm"
axios.defaults.baseURL = "http://localhost:14000"

axios.defaults.withCredentials = true
//添加路由守卫
router.beforeEach(function(to,from,next){
      let user =  getSessionStorage("user");
      //判断什么时候不需要跳转到登陆组件
      let topath = to.path;
      if(topath != "/" && topath!="/home" && topath != "/businesslist" && topath != "/businessinfo" && topath != "/login" && topath != "/register" && topath!='/error403'){
          if(user == null){
                //跳转到登陆
                //编程式路由
                router.push("/login");
                location.reload();
          }else{
             next();
          }
      }
      next();
})

Vue.config.productionTip = false
Vue.use(ElementUI);
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

//设置响应拦截器

axios.interceptors.response.use(function(response){

  if(response.data.code==403){

    location.href = '/error403';

  }

  return response;

},function(error){

  console.log(error);

  return Promise.reject(error);

});